package com.cws.controller.sys;

import javax.servlet.http.HttpServletRequest;

import com.cws.dto.CWSToken;
import com.cws.enmu.HeaderKey;
import com.cws.enmu.RspCode;
import com.cws.service.AuthService;
import com.cws.sys.Mem;
import com.cws.utils.WSUtil;
import com.jfinal.core.Controller;

public class AuthController extends Controller {

	public void login() {

		HttpServletRequest req = getRequest();
		String loginName = req.getHeader(HeaderKey.X_LoginName.name());
		String password = req.getHeader(HeaderKey.X_Password.name());

		boolean result = AuthService.getInstance().login(loginName, password);

		if (result) {

			CWSToken token = new CWSToken();
			token.setTokenId(WSUtil.getUUID());
			// 将token放到内存
			Mem.addToken(token.getTokenId());

			renderJson(WSUtil.createRsp(RspCode.SUCCESS, token));

		} else {

			renderJson(WSUtil.createRsp(RspCode.AUTH_FAILED, null));
		}

	}
}
